//
//  XMVideoLevelViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/12.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMVideoLevelViewController.h"
#import "XMVideoLevelSetTableViewCell.h"
#import "XMVideoLevelRuleTableViewCell.h"
#import "XMVideoLevelModel.h"
#import "XMVideoLevelDownTipView.h"


@interface XMVideoLevelViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *lastWeekTimeLa;
@property (weak, nonatomic) IBOutlet UILabel *currentLevelLa;
@property (weak, nonatomic) IBOutlet UILabel *totleTimeLa;
@property (weak, nonatomic) IBOutlet UILabel *ruleLa;
@property (weak, nonatomic) IBOutlet UITableView *priceTableView;
@property (weak, nonatomic) IBOutlet UITableView *ruleTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *priceTableH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ruleTableH;
@property (nonatomic, strong) XMVideoLevelDownTipView *downTipView;


@property (nonatomic, strong) XMVideoLevelModel *model;
@end

@implementation XMVideoLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self initData];
}

- (void)initData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/videoLevelData" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            XMVideoLevelModel *model = [XMVideoLevelModel mj_objectWithKeyValues:result];
            self.model = model;
            self.priceTableH.constant = self.model.table.count * 32;
            self.ruleTableH.constant = (self.model.rules.count) * 36;
            
            [self.priceTableView reloadData];
            [self.ruleTableView reloadData];
            
            self.lastWeekTimeLa.text = kStringFormat(@"Lv.%@ %@分钟", model.lastWeekLevel, model.lastWeekDuration);
            
    
            self.currentLevelLa.attributedText = [self mutableAttributedString1:kStringFormat(@"Lv.%@", model.thisWeekLevel) string2:@"当前视频等级"];
            
            self.totleTimeLa.attributedText = [self mutableAttributedString1:kStringFormat(@"%@分钟", model.thisWeekDuration) string2:@"本周累计时长"];
            
            NSString *str = kStringFormat(@"%@\n%@", model.title, model.prove);
            NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.alignment = NSTextAlignmentCenter;
            paragraphStyle.lineSpacing = 4;
            [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attStr.length)];
            self.ruleLa.attributedText = attStr;
            
            if (![XMUtil isBlankString:model.alert]) {
                [self showDownTipView];
            }
        }
    }];
}

- (NSMutableAttributedString *)mutableAttributedString1:(NSString *)string1 string2:(NSString *)string2 {
    NSString *str = kStringFormat(@"%@\n%@", string1, string2);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
        
    [attStr addAttributes:@{
        NSFontAttributeName: kUIFontBold(36),
        NSForegroundColorAttributeName: kHEX(@"#F43A73"),
    } range:[str rangeOfString:string1]];
    
    [attStr addAttributes:@{
        NSFontAttributeName: kUIFontBold(14),
        NSForegroundColorAttributeName: kHEX(@"#663C30"),
    } range:[str rangeOfString:string2]];
    
    if ([str containsString:@"分钟"]) {
        [attStr addAttributes:@{
            NSFontAttributeName: kUIFontBold(22),
            NSForegroundColorAttributeName: kHEX(@"#F43A73"),
        } range:[str rangeOfString:@"分钟"]];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    paragraphStyle.lineSpacing = 4; // 设置行高为10个点
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attStr.length)];
    
    return attStr;
}

#pragma mark - 初始化变量
- (void)initVariables {
    [self.priceTableView registerNib:kNibWithName(kStringFromClass(XMVideoLevelSetTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMVideoLevelSetTableViewCell)];
    [self.ruleTableView registerNib:kNibWithName(kStringFromClass(XMVideoLevelRuleTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMVideoLevelRuleTableViewCell)];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    self.navView.backgroundColor = kHEX(@"#F2F2F2");
    [self.navLeft2Btn setTitle:@"视频等级" forState:UIControlStateNormal];
    kCornerRadius(_ruleTableView, 10, 1, kHEX(@"#E0E0E0"));
    
    self.priceTableView.delegate = self;
    self.priceTableView.dataSource = self;
    self.ruleTableView.delegate = self;
    self.ruleTableView.dataSource = self;
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.priceTableView) {
        return self.model.table.count;
    }
    return self.model.rules.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.priceTableView) {
        return 32;
    }
    return 36;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.priceTableView) {
        XMVideoLevelSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMVideoLevelSetTableViewCell class])];
        cell.model = [self.model.table safe_objectAtIndex:indexPath.row];
        return cell;
    }
    XMVideoLevelRuleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMVideoLevelRuleTableViewCell class])];
    cell.lineV.hidden = indexPath.row == self.model.rules.count - 1;
    cell.model = [self.model.rules safe_objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.priceTableView) {
        XMVideoLevelTable *table = [self.model.table safe_objectAtIndex:indexPath.row];
        if (table.state == 2 || table.state == 0) {
            return;
        }
        __block NSNumber *index;
        [self.model.table enumerateObjectsUsingBlock:^(XMVideoLevelTable * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (indexPath.row == idx) {
                    obj.state = 2;
                    index = obj.index;
                } else {
                    if (obj.state == 0) {
                        obj.state = 0;
                    } else {
                        obj.state = 1;
                    }
                }
        }];
        
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{
            @"video_level": index
        }.mutableCopy;
        [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/videoRank/edit" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showHUDMsg:@"设置成功" toView:nil];
                [self.priceTableView reloadData];
            }
        }];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (XMVideoLevelDownTipView *)downTipView {
    if (_downTipView == nil) {
        _downTipView = [XMVideoLevelDownTipView viewFromXib];
        _downTipView.size = CGSizeMake(kScreenWidth, kScreenHeight);
    }
    return _downTipView;
}

- (void)showDownTipView {
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:self.model.alert];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    paragraphStyle.lineSpacing = 6;
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attStr.length)];
    self.downTipView.descLa.attributedText = attStr;
    [GKCover coverFrom:self.view contentView:self.downTipView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
}

@end
